﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Common.Command;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;
using NHibernate.Transform;
using System.Collections;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.Code
{
    // 输入地址：
    // ~/Interface/ajax/CommandService.aspx?key=ExampleLoadList&param={"Type":"ExampleApply",PageIndex:1,PageSize:10}
    // 返回结果：
    // {"ErrorCode":"0000","Error":false,"ErrorMsg":"unkown error","TotalCount":2,"Result":[{"Title":"123","Phone":"321"},{"Title":"123","Phone":"321"}]}

    /// <summary>
    /// ExampleLoadList命令参数
    /// </summary>
    public class ExampleLoadListParameter : ListCommandParameter
    {
        public string Type { get; set; }
    }

    /// <summary>
    /// ExampleLoadList命令调用输出
    /// </summary>
    public class ExampleLoadListResult : CommandResult
    {
    }

    /// <summary>
    /// 返回的数据
    /// </summary>
    public class ExampleLoadListResultData
    {
        public string Title { get; set; }
        public string Phone { get; set; }
    }

    /// <summary>
    /// ExampleLoadList命令
    /// </summary>
    public class ExampleLoadListCommand : SessionCommand
    {
        public override CommandResult Execute(NHibernate.ISession session, CommandParameter parameter)
        {
            ExampleLoadListResult result = new ExampleLoadListResult();
            ExampleLoadListParameter param = parameter as ExampleLoadListParameter;

            if (param != null)
            {
                //在这里添加命令内容
                QueryFilterList filters = new QueryFilterList();

                string fromSql = " from Message_Info where 1 = 1 ";

                if (!string.IsNullOrWhiteSpace(param.Type))
                {
                    filters.Add(new StringFilter("Type", "Type", param.Type));
                }

                string condtion = "";

                foreach (DataFilter filter in filters)
                {
                    condtion += " and " + filter.GetSection();
                }

                string select = @" select Title, Phone ";

                //获取数据
                IQuery dataquery = session.CreateSQLQuery(select + fromSql + condtion + " order by Recid asc ");

                filters.SetPager(dataquery, param.PageIndex, param.PageSize);
                filters.SetUp(dataquery);

                result.Result = dataquery.SetResultTransformer(new ObjectResultTransformer(typeof(ExampleLoadListResultData))).List<ExampleLoadListResultData>();

                dataquery = session.CreateSQLQuery("select count(*) " + fromSql + condtion);
                filters.SetUp(dataquery);
                result.TotalCount = Convert.ToInt32(dataquery.UniqueResult());
            }
            else
            {
                result.ErrorCode = "0001";
            }
            return result;
        }

        protected override void PrepareExecute()
        {
            //在这里添加执行命令前的操作
        }

        protected override void AfterExecute()
        {
            //在这里添加执行命令后的操作
        }
    }
}
